package conexao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConexaoMySQL {
	
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	
	private String url = "jdbc:mysql://";
	private String server = "localhost";
	private String port = "3306";
	private String database;
	private String user;
	private String password;
	
	private Connection connection;

	public ConexaoMySQL(String server, String port, String database, String user, String password)
	{
		setServer(server);
		setPort(port);
		setDatabase(database);
		setUser(user);
		setPassword(password);
	}
	
	public ConexaoMySQL(String database, String user, String password)
	{
		setUser(user);
		setPassword(password);
		setDatabase(database);	
	}

	public Connection conexao() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException 
	{
		try{
			Class.forName(DRIVER);
			connection = DriverManager.getConnection(url + getServer() + ":" + getPort() + "/" + getDatabase(), getUser(), getPassword());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return connection;
	}
	
	public void fecha() throws SQLException
	{
		connection.close();
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getDatabase() {
		return database;
	}

	public void setDatabase(String database) {
		this.database = database;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
